package cn.yhbk.web.modules.manager.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.DTO.Major.MajorAll;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbCollege;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbMajor;
import cn.yhbk.web.modules.manager.Pojo.VO.Major.MajorAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.Major.MajorPag;
import cn.yhbk.web.modules.manager.Pojo.VO.Major.MajorUpdate;
import cn.yhbk.web.modules.manager.Service.ITbCollegeService;
import cn.yhbk.web.modules.manager.Service.ITbMajorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 专业表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Api(tags = "专业接口")
@Slf4j
@RestController
@RequestMapping("/manager/tbMajor")
public class TbMajorController {

    @Autowired
    private ITbMajorService iTbMajorService;

    @Autowired
    private ITbCollegeService tbCollegeService;

    @ApiOperation("根据ID查询专业")
    @GetMapping("/getMajorInfo")
    public CommonResult<TbMajor> getMajorInfo(@RequestParam("id") Long id){
        log.info("查询专业,id:" + id);
        return CommonResult.success(iTbMajorService.getById(id));
    }

    @ApiOperation("增加专业")
    @PostMapping("/saveMajor")
    public CommonResult<String> saveMajor(@RequestBody MajorAdd majorAdd){
        log.info("增加专业，" + majorAdd);
        // 获取账号
        Long currentAdminId = MyUtil.getCurrentAdminId();
        // 获取学院ID
        QueryWrapper<TbCollege> tbCollegeQueryWrapper = new QueryWrapper<>();
        tbCollegeQueryWrapper.eq("stu_work_office_id", currentAdminId);
        TbCollege tbCollege = tbCollegeService.getOne(tbCollegeQueryWrapper);
        // 保存
        TbMajor tbMajor = BeanUtil.copyProperties(majorAdd, TbMajor.class);
        tbMajor.setCollegeId(tbCollege.getId());
        Boolean saveMajor = iTbMajorService.save(tbMajor);
        if(!saveMajor){
            return CommonResult.failed("保存专业出错");
        }
        return CommonResult.success("保存成功");
    }

    @ApiOperation("修改专业")
    @PutMapping("/updateMajor")
    public CommonResult<String> updateMajor(@RequestBody MajorUpdate majorUpdate){
        log.info("修改专业，" + majorUpdate);
        TbMajor tbMajor = BeanUtil.copyProperties(majorUpdate, TbMajor.class);
        tbMajor.setUpdateTime(LocalDateTime.now());
        Boolean updateMajor = iTbMajorService.updateById(tbMajor);
        if(!updateMajor){
            return CommonResult.failed("修改专业出错");
        }
        return CommonResult.success("修改成功");
    }

    @ApiOperation("删除专业")
    @DeleteMapping("/deleteMajor/{majorId}")
    public CommonResult<String> deleteMajor(@PathVariable("majorId") Long id){
        log.info("删除专业，id:" + id);
        Boolean deleteGrade= iTbMajorService.removeById(id);
        if(!deleteGrade){
            return CommonResult.failed("删除专业出错");
        }
        return CommonResult.success("修改成功");
    }

    @ApiOperation("分页获取全部专业")
    @GetMapping("/getMajorInfoPag")
    public CommonResult<MajorPag> ManageGetMajorInfo(@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                     @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize,
                                                     @RequestParam(name = "collegeId") Long collegeId){
        MajorPag majorPag = iTbMajorService.ManageGetMajorInfo(currentPage, pageSize, collegeId);
        return CommonResult.success(majorPag);
    }

    @ApiOperation("学工办分页获取自己学院的全部专业")
    @GetMapping("/getMajorInfoStuOffPag")
    public CommonResult<MajorPag> getMajorInfoStuOffPag(@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                                     @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize){
        MajorPag majorPag = iTbMajorService.getMajorInfoStuOffPag(currentPage, pageSize);
        return CommonResult.success(majorPag);
    }


    @ApiOperation("查询所有专业，专业中包括年级，年级包括各个专业")
    @GetMapping("/getMajorAll")
    public CommonResult<List<MajorAll>> getMajorAll(){
        List<MajorAll> majorAll = iTbMajorService.getMajorAll();
        return CommonResult.success(majorAll);
    }
}
